/**
 * 
 */
package dao.hibernate;

import filesystem.MusicFinder;
import hibernate.Album;

import java.util.List;

import model.IAlbum;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import dao.IAlbumDAO;

/**
 * @author ipeel
 *
 */
public class AlbumDAO extends BaseHibernateDAO implements IAlbumDAO {
	@SuppressWarnings("unchecked")
	public List<IAlbum> getAlbumsByArtistId(Integer artistId) {
		return sessionFactory.getCurrentSession()
						.createCriteria(Album.class)
						.add(Restrictions.eq("artist.id", artistId))
						.addOrder(Order.asc("name"))
						.list();
	}

	/* (non-Javadoc)
	 * @see dao.IAlbumDAO#getAlbumById(java.lang.Integer)
	 */
	public IAlbum getAlbumById(Integer albumId) {
		return (IAlbum)sessionFactory.getCurrentSession().load(Album.class, albumId);
	}
	
	@SuppressWarnings("unchecked")
	public List<IAlbum> getAlbumsByArtistName(String name) {
		return sessionFactory.getCurrentSession()
						.createCriteria(Album.class)
						.add(Restrictions.like("artist.name", name))
						.addOrder(Order.asc("name"))
						.list();
	}

	/* (non-Javadoc)
	 * @see dao.IAlbumDAO#getAlbumsFromFileSystem(java.lang.String)
	 */
	public MusicFinder getAlbumsFromFileSystem(String path) {
		MusicFinder musicFinder = new MusicFinder();
		musicFinder.parseFiles(path);
		
		return musicFinder;
	}

	/* (non-Javadoc)
	 * @see dao.IAlbumDAO#saveAlbum(model.IAlbum)
	 */
	public void saveAlbum(IAlbum album) {
		sessionFactory.getCurrentSession().saveOrUpdate(album);		
	}

	/* (non-Javadoc)
	 * @see dao.IAlbumDAO#getAlbumByName(java.lang.String)
	 */
	public IAlbum getAlbumByName(String name) {
		return (IAlbum)sessionFactory.getCurrentSession()
		.createCriteria(Album.class)
		.add(Restrictions.eq("name", name))
		.uniqueResult();
	}
}